package com.tql.pandian.controller;


import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.tql.pandian.entity.TqlStapInChangeEntity;
import com.tql.pandian.entity.analysis.TqlCaseAnlasysEntity;
import com.tql.pandian.entity.analysis.dto.BillFoodReportDayDto;
import com.tql.pandian.entity.analysis.dto.SaleDataDTO;
import com.tql.pandian.entity.analysis.vo.FoodReportVo;
import com.tql.pandian.entity.dto.StageOperationDTO;
import com.tql.pandian.entity.dto.StockDTO;
import com.tql.pandian.entity.dto.TargetManageDTO;
import com.tql.pandian.entity.vo.CaseAnalsysDto;
import com.tql.pandian.service.IAnotherDataBaseService;
import com.tql.pandian.service.ICostAnalysisService;
import com.tql.pandian.util.CommonUtilHll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Api("统计数据接口")
@RestController
@RequestMapping("/costAnalysis")
public class CostAnalysisController extends BaseController {

    @Autowired
    private ICostAnalysisService costAnalysisService;

    @ApiOperation("销售数据展示")
    @Log(title = "销售数据展示", businessType = BusinessType.OTHER)
    @GetMapping("/getSaleData")
    public TableDataInfo getSaleData(@ApiIgnore @RequestParam Map<String, Object> params){
        startPage();
        List<BillFoodReportDayDto> list = costAnalysisService.getSaleData(params);
        return getDataTable(list);
        //return success(costAnalysisService.getSaleData(dto));
    }

    @ApiOperation("分析展示-库存分析")
    @Log(title = "分析展示-库存分析", businessType = BusinessType.OTHER)
    @GetMapping("/getStock")
    public TableDataInfo getStock(StockDTO dto) throws ParseException {
        String shopCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
        dto.setDeptCode(shopCode);
        if (StringUtils.isEmpty(dto.getParamTime())){
            dto.setParamTime(CommonUtilHll.getMonthDate(new Date()));
        }
        startPage();
        List<TqlCaseAnlasysEntity> list = costAnalysisService.getStock(dto);
        if (list.isEmpty()) {
            return getDataTable(list);
        }
        List<TqlCaseAnlasysEntity> list1 =costAnalysisService.findStapNameAndDeptName(list,1);
        Map<String, String> map1 = list1.stream().collect(Collectors.toMap(TqlCaseAnlasysEntity::getStapCode, TqlCaseAnlasysEntity::getStapName));
        //stapName赋值
        list.forEach(each -> {
            String stapCode = each.getStapCode();
            String stapName = map1.get(stapCode);
            each.setStapName(stapName);
        });
        //deptName赋值
        List<TqlCaseAnlasysEntity> list2 =costAnalysisService.findStapNameAndDeptName(list,2);
        Map<String, String> map2 = list2.stream().collect(Collectors.toMap(TqlCaseAnlasysEntity::getDeptCode, TqlCaseAnlasysEntity::getDeptName));
        list.forEach(each -> {
            String deptCode = each.getDeptCode();
            String deptName = map2.get(deptCode);
            each.setDeptName(deptName);
        });
        return getDataTable(list);
    }

    @ApiOperation("分析展示-目标管理-折扣前")
    @Log(title = "分析展示-目标管理-折扣前", businessType = BusinessType.OTHER)
    @PostMapping("/getTargetManageDiscount")
    public AjaxResult getTargetManageDiscount(@RequestBody(required = false) TargetManageDTO dto){
        return success(costAnalysisService.getTargetManageDiscount(dto));
    }

    @ApiOperation("分析展示-目标管理-实际毛利率")
    @Log(title = "分析展示-目标管理-实际毛利率", businessType = BusinessType.OTHER)
    @PostMapping("/getTargetManageReal")
    public AjaxResult getTargetManageReal(@RequestBody(required = false) TargetManageDTO dto){
        return success(costAnalysisService.getTargetManageReal(dto));
    }

    @ApiOperation("分析展示-阶段运营分析")
    @Log(title = "分析展示-阶段运营分析", businessType = BusinessType.OTHER)
    @PostMapping("/getStageOperation")
    public AjaxResult getStageOperation(@RequestBody StageOperationDTO dto){
        return success(costAnalysisService.getStageOperation(dto));
    }



    @ApiOperation("分析展示-阶段运营分析1")
    @Log(title = "分析展示-阶段运营分析1", businessType = BusinessType.OTHER)
    @PostMapping("/getStageOperation1")
    public AjaxResult getStageOperation1(@RequestBody StageOperationDTO dto){
        return success(costAnalysisService.getStageOperation1(dto));
    }



    @ApiOperation("库存分析物料采购展示")
    @Log(title = "库存分析物料采购展示", businessType = BusinessType.OTHER)
    @PostMapping("/getCaseAnalsysIninfo")
    public AjaxResult  getCaseAnalsysIninfo(@RequestBody CaseAnalsysDto dto){
        int month= dto.getInmonth();
        String inids=dto.getInids();
        List<TqlStapInChangeEntity> list=costAnalysisService.getCaseAnalsysIninfo(inids,month);
        return success(list);
    }

}
